app.controller("featureMapCtrl", ['$scope', '$http', '$stateParams', '$modal', '$rootScope', '$filter', '$timeout', 'lodash', function ($scope, $http, $stateParams, $modal, $rootScope, $filter, $timeout, lodash) {
    $scope.info = {};
    $scope.info.diagramScope = {};
    $scope.info.selectInfo = {}
    $scope.info.meneList = [];
    $scope.info.pageList = [];
    $scope.info.featureList = [];
    $scope.info.mapDatas = [];
    $scope.info.search = ""
    $scope.info.showName = "map";
    // $scope.LegendSys = config_flow_legend_sys;
    $scope.info.sys = {"crtname": "请选择系统"};


    $scope.setBreadcrumb = function (title) {
        $rootScope.initBreadcrumb($scope.breadcrumb);
        if (title) {
            $scope.breadcrumb.push('功能地图', title);
        } else {
            $scope.breadcrumb.push('功能地图');
        }
    }
    $scope.setBreadcrumb();

    $scope.initSetting = function () {
        $rootScope.initHeaderElements($scope.settings);
        $scope.settings.btnEle.push({
            show: true,
            label: "返回",
            func: function () {
                $rootScope.backHistory();
            }
        });
        // var sysSelect = {
        //     status: true,
        //     items: $scope.LegendSys,
        //     prompt: "请选择所属产品",
        //     selectedItem: null,
        //     func: function (item) {
        //         $scope.selectLegendSysItem(item);
        //     }
        // };
        // $scope.settings.uiSltEle.push(sysSelect);
    }

    $scope.initSetting();

    $scope.addNodeToTree = function (data) {
        // var node = setFlowTree([data], [])[0];

        $scope.info.diagramScope.createNode(data);
    }

    var featureMaps = getStorage('featureMaps');
    if (featureMaps != undefined && featureMaps != "") {
        $scope.info.sys = featureMaps.sys;
        $scope.info.search = featureMaps.search;
    }

    //解析流程树
    function setFlowTree(flowData, flowArray) {
        angular.forEach(flowData, function (flowItem, index) {
            flowItem.key = flowItem.id;
            flowItem.parent = flowItem.parentId;
            flowArray.push(flowItem);
            if (flowItem.children && flowItem.children.length) {
                setFlowTree(flowItem.children, flowArray);
            }
        });
        return flowArray;
    }

    //获取数据刷新Map信息
    $scope.switchData = function () {
        if (!$scope.info.sys.value) {
            return;
        }
        $scope.info.selectInfo = {}
        var list = []
        $rootScope.showPanelLoading();
        $.ajax({
            type: "get",
            url: matrix_product_url + "/feature/getAllElements?sys=" + $scope.info.sys.value,
            async: true,
            success: function (data) {
                $scope.info.meneList = data.data.menus;
                $scope.info.pageList = data.data.pages;
                $scope.info.featureList = data.data.features;
                if (data.code == 0) {
                    angular.forEach(data.data.menus, function (menu) {
                        menu = initMenu(menu)
                        list.push(menu)
                    })
                    angular.forEach(data.data.pages, function (page) {
                        page = initPage(page)
                        list.push(page)
                    })
                    angular.forEach(data.data.features, function (feature) {
                        feature = initFeature(feature)
                        list.push(feature)
                    })
                    console.log("数据初始化成功")
                    $scope.info.mapDatas = list;
                    $rootScope.$apply();
                } else {
                    $rootScope.toasterErrorInfo(result.desc);
                }
                $rootScope.hidePanelLoading();
            }
        });
    }

    //初始化菜单数据;
    function initMenu(menu) {
        menu.key = menu.id;
        menu.parent = menu.parent_id;
        menu.name = menu.menu_name;
        menu.mapType = "MENU"
        if (menu.level == 0) {
            menu.mapType = "SYS"
        }
        return menu;
    }

    //初始化Page数据
    function initPage(page) {
        page.key = page.id;
        page.parent = page.menuId;
        page.name = page.title;
        page.mapType = "PAGE"
        return page;
    }

    //初始化feature数据
    function initFeature(feature) {
        feature.key = feature.id
        feature.parent = feature.parent_id
        feature.name = feature.featureName;
        feature.mapType = feature.type == 'PROTOTYPE' ? "PROTOTYPE" : "FEATURE";
        return feature;
    }

    $scope.switchData();

    //更新方法
    $scope.update = function () {
        if ($scope.info.selectInfo.mapType == "MENU") {
            menuModal("update")
        }
        if ($scope.info.selectInfo.mapType == "PAGE") {
            pageModal($scope.info.selectInfo, "update")
        }
        if ($scope.info.selectInfo.mapType == "FEATURE") {
            $scope.addFeature("update")
        }
    }

    //选择产品事件
    $scope.selectLegendSysItem = function (item) {
        $scope.info.sys = item;
        $scope.switchData();
    }

    //点击图例事件
    $scope.selectMap = function (info) {
        $scope.info.selectInfo = info;
        $scope.$apply();
    }
    //查看历史记录
    $scope.history = function () {
        $scope.info.showName = "history"
        $scope.info.datas = {};
        $scope.info.featureName = "";
        $rootScope.initHeaderElements($scope.settings);
        var backUrl = {
            show: true,
            label: "返回",
            func: function () {
                $scope.info.showName = "map"
                $scope.setBreadcrumb();
                $scope.initSetting();
            }
        };
        $scope.settings.btnEle.push(backUrl);

        $http.get(matrix_product_url + "/feature/getAllFeatureVersions?featureId=" + $scope.info.selectInfo.id).success(function (result) {
            if (result.code == 0) {
                $scope.info.featureName = result.data.master[0].attrs.feature_name;
                if ($scope.info.featureName) {

                    $scope.setBreadcrumb("历史记录>>" + $scope.info.featureName);
                } else {
                    $scope.setBreadcrumb("历史记录");
                }
                $scope.info.showVersion = false;
                $scope.info.sltPrevItem = "";
                $scope.info.sltNextItem = "";
                var versionArray = [];
                if (result.data.master.length > 0) {
                    angular.forEach(result.data.master, function (master) {
                        versionArray.push(master);
                    });
                }
                if (result.data.req.length > 0) {
                    angular.forEach(result.data.req, function (req) {
                        versionArray.push(req);
                    });
                }
                if (!versionArray) {
                    $rootScope.toasterWarningInfo("流程版本数据为空！");
                    return false;
                }
                angular.forEach(versionArray, function (ver) {
                    ver.crtname = "【" + (ver.reqName == "master" ? "master" : ver.reqName) + "】" + ver.version;
                });
                $scope.info.flowVersions = versionArray;
                var vNodeItemArray = [];
                var vLinkItemArray = [];
                var masterLinkArray = [];
                var filterLinkArray = [];
                angular.forEach(versionArray, function (version) {
                    var vNodeItem = {
                        num: lodash.trim(version.version, (version.reqId == "-1" ? "V" : "v")),
                        key: version.reqId == "-1" ? version.attrs.version : lodash.trim(version.version, "v"),
                        name: version.reqId == "-1" ? version.crtname : ("【" + version.reqName + "】" + version.version),
                        type: version.reqId == "-1" ? "master" : "req",
                        color: version.reqId == "-1" ? "#bdd0e1" : "#deefcd",
                        attrs: version.attrs,
                    };
                    if (version.version == "online") {
                        vNodeItem.color = "#fff7b6";
                    }
                    if (version.reqId != "-1") {
                        vNodeItem.key = lodash.trim(version.version, "v") + version.reqId;
                        vNodeItem.reqId = version.reqId;
                        filterLinkArray.push(vNodeItem);
                    }
                    vNodeItemArray.push(vNodeItem);
                });

                vNodeItemArray = $filter('orderBy')($filter('orderBy')(vNodeItemArray, "num", false), "type", false)
                angular.forEach(vNodeItemArray, function (vNodeItem, index) {
                    if (vNodeItem.type == "master") {
                        if (vNodeItem.num == "online") {
                            return;
                        }
                        var vLinkItem = {
                            from: vNodeItem.attrs.version,
                            to: vNodeItemArray[index + 1].attrs.version
                        };
                        vLinkItemArray.push(vLinkItem);
                    }
                })

                var reqArray = [];
                var filterMasterLink = [];
                filterMasterLink = $filter('orderBy')(masterLinkArray, "num", false);
                for (var i = 0; i < filterMasterLink.length; i++) {
                    if (i + 1 != filterMasterLink.length) {
                        vLinkItemArray.push({from: filterMasterLink[i].key, to: filterMasterLink[i + 1].key});
                    }
                }
                angular.forEach(filterLinkArray, function (filterItem) {
                    var reqIndex = lodash.findIndex(reqArray, function (req) {
                        return filterItem.reqId == req.reqId;
                    });
                    if (reqIndex == -1) {
                        reqArray.push({"reqId": filterItem.reqId});
                    }
                });
                angular.forEach(reqArray, function (req) {
                    var crtReqArray = [];
                    angular.forEach(filterLinkArray, function (item) {
                        if (item.reqId == req.reqId) {
                            crtReqArray.push(item);
                        }
                    });
                    for (var i = 0; i < crtReqArray.length; i++) {
                        var linkList = [];
                        angular.forEach(filterLinkArray, function (fl) {
                            if (fl.reqId == crtReqArray[i].reqId) {
                                linkList.push(fl)
                            }
                        })
                        var firstReqItem = lodash.first($filter('orderBy')(linkList, "num", false));
                        if (crtReqArray[i].reqId == req.reqId) {
                            if (i + 1 != crtReqArray.length) {
                                vLinkItemArray.push({
                                    from: crtReqArray[i].num + req.reqId,
                                    to: crtReqArray[i + 1].num + req.reqId
                                });
                            }
                            if (crtReqArray[i].num == firstReqItem.num) {
                                vLinkItemArray.push({
                                    from: crtReqArray[i].attrs.from_master_version,
                                    to: crtReqArray[i].num + req.reqId
                                });
                            }
                            if (i == crtReqArray.length - 1) {
                                vLinkItemArray.push({
                                    from: crtReqArray[i].num + req.reqId,
                                    to: crtReqArray[i].attrs.to_master_version
                                });
                            }
                        }
                    }
                });
                $scope.info.datas = {"vNodeItemArray": vNodeItemArray, "vLinkItemArray": vLinkItemArray};
            } else {
                $rootScope.toasterErrorInfo(result.desc);
            }
        })

        $scope.select = function (info) {
            $scope.info.historySelectInfo = info;
            $scope.$apply();
        }


        $scope.compareHistory = function () {
            if (!$scope.info.sltPrevItem) {
                $rootScope.toasterWarningInfo("版本数据不能为空，请选择！");
                return false;
            }
            if (!$scope.info.sltNextItem) {
                $rootScope.toasterWarningInfo("版本数据不能为空，请选择！");
                return false;
            }
            getPrevHistory($scope.info.sltPrevItem);
            getNextHistory($scope.info.sltNextItem);
            $scope.info.showVersion = true;
        };

        //获取前一个版本的历史数据
        function getPrevHistory(prevHistoryDetails) {
            $scope.info.prevPanel = prevHistoryDetails.attrs
        };

        //获取后一个版本的历史数据
        function getNextHistory(nextHistoryDetails) {
            $scope.info.nextPanel = nextHistoryDetails.attrs
        };
        $scope.view = function () {
            $scope.info.showVersion = false;
        }
        // setStorage('featureMaps', {"sys":$scope.info.sys,"search":$scope.info.search})
        // window.location.href = "#/app/matrix/historyFeatureMap/"+ $scope.info.selectInfo.id;
    }

    //查看功能点
    $scope.findFeature = function () {
        $scope.info.showName = "find";
        $rootScope.initHeaderElements($scope.settings);
        var backUrl = {
            show: true,
            label: "返回",
            func: function () {
                $scope.info.showName = "map";
                $scope.setBreadcrumb();
                $scope.initSetting();
            }
        };
        $scope.settings.btnEle.push(backUrl);
        $scope.info.featureId = $scope.info.selectInfo.id;

        $scope.info.featureInfo = {};
        $http.get(matrix_product_url + "/feature/getFeature?id=" + $scope.info.featureId + "&sys=" + $scope.info.sys.value + "&reqId=-1&withPrototype=1&withAttr=0").success(function (result) {
            if (result.code == "0") {
                $scope.info.featureInfo = result.data != null ? result.data [0] : {};
                $scope.setBreadcrumb("查看>>" + $scope.info.featureInfo.featureName);
            } else {
                $rootScope.showMessage("error", "失败", result.desc);
            }
        })
        // setStorage('featureMaps', {"sys":$scope.info.sys,"search":$scope.info.search})
        // window.location.href = "#/app/matrix/findFeature/"+ $scope.info.selectInfo.id+"/-1";
    }

    //创建菜单
    $scope.addMenu = function () {
        menuModal("add");
    };

    //创建菜单弹窗
    function menuModal(type) {
        var modalInstance = $modal.open({
            animation: true,
            templateUrl: getTemplateUrl("matrix/component/tpl/menuModal.html"),
            controller: "addMapMenuCtrl",
            keyboard: true,
            backdrop: true,
            resolve: {
                status: function () {
                    return type == 'add' ? true : false;
                },
                isFlowTree: function () {
                    return false;
                },
                menu: function () {
                    return $scope.info.selectInfo;
                },
                type: function () {
                    return type;
                }
            }
        });

        //点击modal新增后，把值传过来
        modalInstance.result.then(
            function (menu) {
                createMenu(menu)
            }, function (reason) {
                cancelModal(reason);
            });
    };

    //取消
    function cancelModal() {
        console.log("cancelModal");
    };

    //创建menu节点
    function createMenu(data) {
        var url = matrix_product_url + "/menu/save";
        var postParams = {
            "is_del": $scope.info.selectInfo.is_del,
            "level": data.status ? parseInt($scope.info.selectInfo.level) + 1 : parseInt($scope.info.selectInfo.level),
            "status": data.status ? "ADD" : "UPDATE",
            "req_id": $scope.info.selectInfo.req_id,
            "sys": $scope.info.selectInfo.sys,
            "parent_id": data.parent,
            "menu_name": data.name,
            "menu_data": {
                "name": data.name,
                "path": data.path,
                //"routeId" : path + "/" + menu.path
                "routeId": $scope.info.selectInfo.menu_data.routeId
            }
        };
        data.status ? postParams : postParams.id = $scope.info.selectInfo.id;
        $http({
            method: "post",
            url: url,
            data: JSON.stringify(postParams),
            headers: {"Content-Type": "application/json"}
        }).success(function (result) {
            if (result.code == "0") {
                if (!result.data) {
                    $rootScope.toasterErrorInfo("创建流程，返回数据错误！");
                }
                if (data.status) {
                    var menuData = initMenu(result.data);
                    menuData.path = menuData.menu_data.routeId;
                    $scope.info.meneList.push(menuData);
                    $scope.addNodeToTree(initMenu(result.data));
                } else {
                    if (data.operationType == "move") {
                        $scope.info.diagramScope.moveNode(data);
                    } else {
                        $scope.info.selectInfo.name = postParams.menu_name;
                        $scope.info.diagramScope.updateNode();
                    }
                }

                $rootScope.toasterSuccessInfo(data.status ? "创建成功！" : "修改成功！");
            } else {
                $rootScope.toasterErrorInfo(result.desc);
            }
        });
    };
    //点击新增按钮，打开modal---page
    $scope.addPage = function () {
        pageModal($scope.info.selectInfo, 'add');
    }

    function pageModal(selectedBranch, type) {
        var modalInstance = $modal.open({
            animation: true,
            templateUrl: getTemplateUrl("matrix/component/tpl/pageModal.html"),
            controller: "addMapPageModalCtrl",
            keyboard: true,
            backdrop: true,
            resolve: {
                branch: function () {
                    return selectedBranch;
                },
                status: function () {
                    return type == 'add' ? true : false;
                }
            }
        });

        //点击modal新增后，把值传过来
        modalInstance.result.then(
            function (page) {
                createPageModal(page);
            },
            function (reason) {
                cancelModal(reason);
            }
        );
    };

    //modal按钮新增----page
    function createPageModal(page) {
        var pageId = page.status ? "" : $scope.info.selectInfo.id;
        var postParams = {
            "id": pageId,
            "title": page.name,
            "fileName": page.status ? "" : page.fileName,
            "models": "",
            "menuId": page.pageMenuId,
            "menuPath": page.routeId,
        };
        var url = matrix_product_url + "/page/savePage";
        $http({
            method: "post",
            url: url,
            data: JSON.stringify(postParams),
            headers: {"Content-Type": "application/json"}
        }).success(function (result) {
            if (result.code == "0") {
                if (page.status) {
                    $scope.addNodeToTree(initPage(result.data));
                    $http.get(matrix_product_url + "/feature/getPageFeatures?pageId=" + result.data.id).success(function (featureData) {
                        if (featureData.code == "0") {
                            featureData.data[0].parent_id = featureData.data[0].pageId;
                            var feature = initFeature(featureData.data[0]);
                            $scope.addNodeToTree(feature);
                        }
                    })
                } else {
                    if (page.operationType == "move") {
                        $scope.info.diagramScope.moveNode(page);
                    } else {
                        $scope.info.selectInfo.name = postParams.title;
                        $scope.info.diagramScope.updateNode();
                    }
                }
                $rootScope.showMessage("success", "成功", (page.status ? "创建成功！" : "修改成功！"));
            } else {
                $rootScope.showMessage("error", "失败", result.desc);
            }
        });
    };

    //添加功能点

    $scope.addFeature = function (type) {
        var modalInstance = $modal.open({
            animation: true,
            templateUrl: getTemplateUrl("matrix/component/tpl/featureModal.html"),
            controller: "addFeatureMapCtrl",
            keyboard: true,
            backdrop: true,
            resolve: {
                branch: function () {
                    return $scope.info.selectInfo;
                },
                status: function () {
                    return type == 'add' ? true : false;
                }
            }
        });

        //点击modal新增后，把feature值传过来
        modalInstance.result.then(function (feature) {
            createFeature(feature);
        }, function (reason) {
            cancelModal(reason);
        });
    };

    //创建功能点（新增）
    function createFeature(feature) {
        var postParams = {
            "sys": $scope.info.sys.value,
            "path": feature.path,
            "reqId": "-1",
            "pageId": feature.pageId,
            "originalPageId": feature.originalPageId,
            "featureName": feature.name,
            "attr": {
                "feature_name": feature.name
            },
            "featureKind": feature.featureKind
        };
        feature.status ? postParams : postParams.id = $scope.info.selectInfo.id;
        var url = matrix_product_url + "/feature/createFeature";
        $http({
            method: "post",
            url: url,
            data: JSON.stringify(postParams),
            headers: {"Content-Type": "application/json"}
        }).success(function (result) {
            if (result.code == "0") {
                // $scope.switchData();
                if (feature.status) {
                    $http.get(matrix_product_url + "/feature/getFeature?id=" + result.data + "&withPrototype=1&withAttr=0").success(function (featureData) {
                        if (featureData.code == "0") {
                            $scope.addNodeToTree(initFeature(featureData.data[0]))
                        }
                    })
                    // $scope.addNodeToTree(initFeature(result.data));
                } else {
                    if (feature.operationType == "move") {
                        $scope.info.diagramScope.moveNode(feature);
                    } else {
                        $scope.info.selectInfo.name = postParams.featureName;
                        $scope.info.diagramScope.updateNode();
                    }

                }
                $rootScope.toasterSuccessInfo(feature.status ? "创建成功！" : "修改成功！");
            } else {
                $rootScope.showMessage("error", "失败", result.desc);
            }
        })
    };


    //删除方法
    $scope.delete = function () {
        if ($scope.info.selectInfo.mapType == "MENU") {
            deleteMenu();
        }
        if ($scope.info.selectInfo.mapType == "PAGE") {
            deletePage();
        }
        if ($scope.info.selectInfo.mapType == "FEATURE") {
            deleteFeature();
        }
    }

    //删除page
    function deletePage() {
        var confirmParams = {
            "titleData": "提示",
            "bodyData": "确定要删除PAGE【" + $scope.info.selectInfo.name + "】节点吗？"
        };
        $rootScope.confirmWindow(confirmParams, function (data) {
            if (data) {
                var deleteId = $scope.info.selectInfo.id;
                if (!deleteId) {
                    $rootScope.showMessage("error", "失败", "pageId未定义！");
                }
                var url = matrix_product_url + "/page/deletePage?pageId=" + deleteId;
                $http.get(url).success(function (result) {
                    if (result.code == "0") {
                        $scope.info.diagramScope.deleteNode(initPage([data], [])[0]);
                        $rootScope.showMessage("success", "成功", "page节点删除成功！");
                    } else {
                        $rootScope.showMessage("error", "失败", result.desc);
                    }
                });
            }
        });
    };

    //删除menu
    function deleteMenu() {
        var confirmParams = {
            "titleData": "提示",
            "bodyData": "确定要删除MENU【" + $scope.info.selectInfo.name + "】节点吗？"
        };
        $rootScope.confirmWindow(confirmParams, function (data) {
            if (data) {
                var deleteId = $scope.info.selectInfo.id;
                if (!deleteId) {
                    $rootScope.showMessage("error", "失败", "menuId未定义！");
                }
                var postParams = {
                    "id": deleteId,
                    "status": "DELETE"
                };
                var url = matrix_product_url + "/menu/save";
                $http({
                    method: "post",
                    url: url,
                    data: JSON.stringify(postParams),
                    headers: {"Content-Type": "application/json"}
                }).success(function (result) {
                    if (result.code == "0") {
                        $scope.info.diagramScope.deleteNode(initMenu([data], [])[0]);
                        $rootScope.showMessage("success", "成功", "menu节点删除成功！");
                    } else {
                        $rootScope.showMessage("error", "失败", result.desc);
                    }
                })
            }
        })
    }

    //删除feature
    function deleteFeature() {
        var confirmParams = {
            "titleData": "提示",
            "bodyData": "确定要删除FEATURE【" + $scope.info.selectInfo.name + "】功能点吗？"
        };
        $rootScope.confirmWindow(confirmParams, function (data) {
            if (data) {
                var url = matrix_product_url + "/feature/deleteFeature?featureId=" + $scope.info.selectInfo.id;
                $http.get(url).success(function (result) {
                    if (result.code == "0") {
                        $scope.info.diagramScope.deleteNode(initFeature([data], [])[0]);
                        $rootScope.showMessage("success", "成功", "删除成功！");
                    } else {
                        $rootScope.showMessage("error", "失败", result.desc);
                    }
                });
            }
        })

    };

    $scope.move = function () {
        if ($scope.info.selectInfo.mapType == "MENU") {
            moveModal($scope.info.meneList);
        }
        if ($scope.info.selectInfo.mapType == "PAGE") {
            moveModal($scope.info.meneList);
        }
        if ($scope.info.selectInfo.mapType == "FEATURE") {
            moveModal($scope.info.pageList);
        }
    }

    //移动
    function moveModal(moveDatas) {
        var modalInstance = $modal.open({
            animation: true,
            templateUrl: getTemplateUrl("matrix/component/tpl/moveMap.html"),
            controller: "moveMapCtrl",
            keyboard: true,
            backdrop: true,
            resolve: {
                branch: function () {
                    return $scope.info.selectInfo;
                },
                status: function () {
                    return false;
                },
                moveDatas: function () {
                    return moveDatas;
                }
            }
        });


        modalInstance.result.then(
            function (data) {
                if ($scope.info.selectInfo.mapType == "MENU") {
                    createMenu(data)
                }
                if ($scope.info.selectInfo.mapType == "PAGE") {
                    createPageModal(data)
                }
                if ($scope.info.selectInfo.mapType == "FEATURE") {
                    createFeature(data)
                }
            },
            function (reason) {
                cancelModal(reason);
            }
        );
    };

    $scope.info.isSort = false;
    $scope.info.childs = [];
    $scope.toSort = function () {
        //获取选中节点的父节点下所有字节点
        $scope.info.childs = $scope.info.diagramScope.getChildsData();
        //隐藏flow 展示排序
        $scope.info.isSort = true;
        //修改返回按钮
        $rootScope.initHeaderElements($scope.settings);
        $scope.settings.btnEle.push({
            show: true,
            label: "返回",
            func: function () {
                $scope.initSetting();
                $scope.info.isSort = false;
            }
        });
    }
    //调整节点顺序
    $scope.sortNode = function (list) {
        var ids = '';
        angular.forEach(list, function (data) {
            ids += data.id + ',';
        })
        console.log(ids)
        console.log($scope.info.selectInfo)
        console.log($scope.info.sys)

        var url = "";
        if ($scope.info.selectInfo.mapType == 'MENU' || $scope.info.selectInfo.mapType == 'PAGE') {
            url = "/menu/childSort";
        } else if ($scope.info.selectInfo.mapType == 'PROTOTYPE' || $scope.info.selectInfo.mapType == 'FEATURE') {
            url = "/page/childSort";
        } else {
            $scope.initSetting();
            $scope.info.isSort = false;
            return;
        }

        $http.post(matrix_product_url + url, {
            id: $scope.info.selectInfo.parent,
            sort: ids
        }).success(function () {
            $scope.selectLegendSysItem($scope.info.sys);
            $scope.initSetting();
            $scope.info.isSort = false;
        }).error(function () {
            $scope.initSetting();
            $scope.info.isSort = false;
        });
    }
}])

app.controller("addMapMenuCtrl", ["$scope", "$rootScope", "$modalInstance", "$http", 'status', 'isFlowTree', 'menu', 'type', function ($scope, $rootScope, $modalInstance, $http, status, isFlowTree, menu, type) {
    $scope.isFlowTree = isFlowTree;
    $scope.menu = {
        name: type == 'add' ? "" : menu.name,
        menuStatus: status,
        status: status,
        parent: status ? menu.id : menu.parent_id,
        path: "",
    };

    $scope.createClick = function () {
        if (isFlowTree) {
            delete $scope.menu.path;
        } else {
            if (!status) {
                $scope.menu.path = menu.menu_data.path;
            }
        }
        $modalInstance.close($scope.menu);
    };

    $scope.cancelClick = function () {
        $modalInstance.dismiss("cancel");
    };
}]);

app.controller("addMapPageModalCtrl", ["$scope", "$rootScope", "$modalInstance", "$http", "branch", "status", function ($scope, $rootScope, $modalInstance, $http, branch, status) {
    $scope.page = {
        name: status ? "" : branch.name,
        pageStatus: status,
        status: status,
        pageMenuId: status ? branch.id : branch.parent_id,
        routeId: status ? branch.menu_data.routeId : branch.menuPath,
        fileName: status ? "" : branch.fileName
    };

    $scope.createClick = function () {
        $modalInstance.close($scope.page);
    };

    $scope.cancelClick = function () {
        $modalInstance.dismiss("cancel");
    };
}]);

app.controller("addFeatureMapCtrl", ["$scope", "$rootScope", "$modalInstance", "$http", "branch", "status", function ($scope, $rootScope, $modalInstance, $http, branch, status) {
    $scope.info = {}
    $scope.info.radioStatus = status ? "true" : "false";
    $scope.info.Radios = true;
    $scope.info.featureTypeList = config_feature_details_type;
    $scope.info.sltFeatureItem = "";
    $scope.feature = {
        name: status ? "" : branch.name,
        status: status,
        pageId: status ? branch.id : branch.parent_id,
        originalPageId: status ? branch.id : branch.parent_id,
        path: status ? branch.menuPath + "/" + branch.title : branch.path
    };

    $scope.selectFeatureType = function(item) {
        $scope.feature.featureKind = item.key;
    }

    //点击新增按钮
    $scope.createClick = function () {
        if(!$scope.feature.featureKind) {
            $rootScope.toasterWarningInfo("类型不能为空！");
            return false;
        }
        if(!$scope.feature.name) {
            $rootScope.toasterWarningInfo("名称不能为空！");
            return false;
        }
        $modalInstance.close($scope.feature);
    };

    //点击取消按钮
    $scope.cancelClick = function () {
        $modalInstance.dismiss("cancel");
    };
}]);


//移动方法
app.controller("moveMapCtrl", ["$scope", "$rootScope", "$modalInstance", "$http", "branch", "status", "moveDatas", function ($scope, $rootScope, $modalInstance, $http, branch, status, moveDatas) {
    $scope.info = {};
    $scope.info.moveDatas = [];
    $scope.info.selectValue = {};
    if (branch.mapType == "MENU") {
        angular.forEach(moveDatas, function (moveData) {
            var data = initMenu(moveData)
            if (data != undefined) {
                data.path = data.menu_data.routeId;
                $scope.info.moveDatas.push(data)
            }
        })
    }
    if (branch.mapType == "PAGE") {
        angular.forEach(moveDatas, function (moveData) {
            // if(moveData.level=="3"){
            moveData.path = moveData.menu_data.routeId;
            $scope.info.moveDatas.push(moveData)
            // }
        })

    }
    if (branch.mapType == "FEATURE") {
        angular.forEach(moveDatas, function (moveData) {
            moveData.path = moveData.menuPath;
        })
        $scope.info.moveDatas = moveDatas;
    }

    function initMenu(moveData) {
        if (branch.level == "3" && moveData.level == "2") {
            return moveData;
        } else if (branch.level == "2" && moveData.level == "1") {
            return moveData
        }
    }


    //保存
    $scope.createClick = function () {
        if (branch.mapType == "PAGE") {
            branch.pageMenuId = $scope.info.selectValue.id;
            branch.routeId = branch.menuPath;
        } else if (branch.mapType == "FEATURE") {
            branch.pageId = $scope.info.selectValue.id;
            branch.originalPageId = $scope.info.selectValue.id;
            branch.path = $scope.info.selectValue.menuPath + "/" + $scope.info.selectValue.title
        } else {
            branch.parent_id = $scope.info.selectValue.id;
        }
        branch.parent = $scope.info.selectValue.id;

        branch.status = status;
        branch.operationType = "move"
        $modalInstance.close(branch);
    };
    //关闭
    $scope.cancelClick = function () {
        $modalInstance.dismiss("cancel");
    };
}]);
